﻿using System;
using Team4Mock.Entity;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Team4Mock.UserControl.Support
{
    public partial class Create : System.Web.UI.UserControl
    {
        SupportEntity s=new SupportEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        btnDel.Visible = true;
                    }
                    else
                    {
                        btnDel.Visible = false;
                    }
                }
                else
                {
                    btnDel.Visible = false;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["s"])))
                {
                    try
                    {
                        CreateSupport.Visible = false;
                        pnEdit.Visible = true;
                        int id = int.Parse(Request.QueryString["s"]);
                        DataTable dt = new DataTable();
                        s.Supportid = id;
                        dt = s.GetData(s);
                        txtUrl2.Text = dt.Rows[0][1].ToString();
                        dlType2.SelectedValue = dt.Rows[0][2].ToString();
                        txtDes2.Text = dt.Rows[0][3].ToString();
                        txtAddBy2.Text = dt.Rows[0][6].ToString();
                        if (dt.Rows[0][7].ToString() == "Yes")
                        {
                            ckactive0.Checked = true;
                        }
                        else
                        {
                            ckactive0.Checked = false;
                        }
                        txtDate2.Text = dt.Rows[0][4].ToString();
                            //System.DateTime.Now.Day + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Year;
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Org"])))
                    {
                        if (!string.IsNullOrEmpty(Convert.ToString(Session["user"])))
                        {
                            CreateSupport.Visible = true;
                            pnEdit.Visible = false;
                            txtAddBy1.Text = Session["user"].ToString();
                            txtDate.Text = System.DateTime.Now.Day + "/" + System.DateTime.Now.Month + "/" +System.DateTime.Now.Year;
                        }
                        else
                        {
                            Response.Redirect("/FormView/Login.aspx");
                        }
                    }
                }
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Organisation.aspx?ID="+Request.QueryString["Org"]);
        }

        protected void SaveSup_Click(object sender, EventArgs e)
        {
            if (txtUrl1.Text=="")
            {
                tb.Text = "Input URL";
            }
            else
            {
                try
                {
                    s.URL = txtUrl1.Text;
                    s.Description = txtDes1.Text;
                    s.UserID = Session["user"].ToString();
                    s.SupMatType = dlType1.SelectedValue;
                    s.OrganizationID = int.Parse(Request.QueryString["Org"]);
                    if (ckactive.Checked == true)
                    {
                        s.IsActive = 1;
                    }
                    else
                        s.IsActive = 0;

                    s.AddedDate = System.DateTime.Now.Month + "/" + System.DateTime.Now.Day + "/" +
                                  System.DateTime.Now.Year;
                    s.Insert(s);
                    tb.Text = "Insert success";
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtUrl2.Text=="")
            {
                lb.Text = "Input URL";
            }
            else
            {
                try
                {
                    s.Supportid = int.Parse(Request.QueryString["s"]);
                    s.URL = txtUrl2.Text;
                    s.Description = txtDes2.Text;
                    s.SupMatType = dlType2.SelectedValue;
                    if (ckactive0.Checked == true)
                    {
                        s.IsActive = 1;
                    }
                    else
                    {
                        s.IsActive = 0;
                    }
                    s.Update(s);
                    lb.Text = "Update success";
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Organisation.aspx?ID=" + Request.QueryString["ID"]);
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(Request.QueryString["s"]);
                s.Supportid = id;
                s.Del(s);
                Response.Redirect("/FormView/Organisation.aspx?ID=" + Request.QueryString["ID"]);
            }
            catch
            {
                lb.Text = "Error while being processed";
            }
        }
    }
}